/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package a1;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Jourdan
 */
/**Includes all things about GridView*/
public class GridView {

    char[][] cell;
    ProjModel pm;
    int gridSize;
    ArrayList<Car> carArrayHor;
    ArrayList<Car> carArrayVer;

    public GridView() {
    }

    /**The constructor of GridView*/
    public GridView(ProjModel pm) {
        this.pm = pm;
        cell = new char[pm.getGridSize()][pm.getGridSize()];
        gridSize = pm.getGridSize();
        carArrayHor = new ArrayList<Car>();
        carArrayVer = new ArrayList<Car>();
    }

    /**Initialize Grid*/
    public void initGrid(char ch, int round) {
        //initialise cell with '?'
        for (int i = 0; i < gridSize; i++) {
            for (int j = 0; j < gridSize; j++) {
                cell[i][j] = ch;
            }
        }
        buildHorLane();
        buildVerLane();
        verBuildCars(pm.getVerLanes());
        horBuildCars(pm.getHorLanes());

        for (int i = 0; i < carArrayHor.size(); i++) {
            runHor(i);
        }
        for (int x = 0; x < carArrayVer.size(); x++) {
            runVer(x);
        }
        showGrid();
    }

    /**the method to show a grid*/
    public void showGrid() {
        for (int i = 0; i < gridSize; i++) {
            for (int j = 0; j < gridSize; j++) {
                System.out.print(cell[i][j] + " ");
            }
            System.out.println();
        }
    }// end method

    /**Build a horizontal lane*/
    public void buildHorLane() {
        int middle = gridSize / 2;
        //if user set horLane = 3
        int horLane = pm.getHorLanes();  //horLane = 3
        for (int i = 0; i < gridSize; i++) {
            for (int c = 0; c < horLane; c++) {
                cell[middle + c][i] = ' ';
            }
        }
    }// end method

    /**Build a vertical lane*/
    public void buildVerLane() {
        int middle = gridSize / 2;
        int verLane = pm.getVerLanes();
        for (int i = 0; i < gridSize; i++) {
            for (int c = 0; c < verLane; c++) {
                cell[i][middle + c] = ' ';
            }
        }
    }// end method
    Random generator = new Random();

    /**Build horizontal cars*/
    public void horBuildCars(int horL) {
        int middle = gridSize / 2; //gridSize 30; middle = 10
        double r = generator.nextDouble() * 1;

        if (r <= pm.getHorProb()) {
            if (horL == 1) {
                carArrayHor.add(new Car(middle, 0));
            } else if (horL == 2) {
                carArrayHor.add(new Car((middle + (int) (Math.random() * 2)), 0));
            } else if (horL == 3) {
                carArrayHor.add(new Car((middle + (int) (Math.random() * 3)), 0));
            }
        }// end if
    }// end method

    /**Build vertical cars*/
    public void verBuildCars(int verL) {
        int middle = gridSize / 2; //gridSize 30; middle = 10
        double r = generator.nextDouble() * 1;

        if (r <= pm.getVerProb()) {
            if (verL == 1) {
                carArrayVer.add(new Car(0, middle));
            } else if (verL == 2) {
                carArrayVer.add(new Car(0, (middle + (int) (Math.random() * 2))));
            } else if (verL == 3) {
                carArrayVer.add(new Car(0, (middle + (int) (Math.random() * 3))));
            } else if (verL == 4) {
                carArrayVer.add(new Car(0, (middle + (int) (Math.random() * 4))));
            }
        }// end if
    }// end method

    /**Run horizontal cars*/
    public void runHor(int cycle) {
        carArrayHor.get(cycle).setYPos((carArrayHor.get(cycle).getYPos()) + 1);
        int xPos = carArrayHor.get(cycle).getXPos();
        int yPos = carArrayHor.get(cycle).getYPos();
        if (yPos >= 30) {
            carArrayHor.remove(cycle);
        } else {
            cell[xPos][yPos] = 'c';
            if (yPos > 0) {
                cell[xPos][yPos - 1] = ' ';
            }
        }
    }// end method

    /**Run vertical cars*/
    public void runVer(int cycle) {
        carArrayVer.get(cycle).setXPos((carArrayVer.get(cycle).getXPos()) + 1);
        int xPos = carArrayVer.get(cycle).getXPos();
        int yPos = carArrayVer.get(cycle).getYPos();
        if (xPos >= 30) {
            carArrayVer.remove(cycle);
        } else {
            cell[xPos][yPos] = 'c';
            if (xPos > 0) {
                cell[xPos - 1][yPos] = ' ';
            }
        }
    }// end method
}
